home *** CD-ROM | disk | FTP | other *** search
- #include "Music4C.h"
- #include "Music4C_mac.h"
- #include "AIFFtype.h"
- #include <SANE.h>
- #include <ToolboxUtil.h>
- #include <unix.h>
-
-
- extern OSErr theErr;
-
- prepareAIFFfile(TotalDuration, nchnls)
- double TotalDuration;
- int nchnls;
- {
- register i;
- double x;
- Str255 myStr255;
- extern OSErr errnum;
- long aLong;
- extern double srate;
- int resid;
- Chunk FormChunkHeader, *FormChunkHeaderPtr;
- CommonChunk CommonHeader, *CommonHeaderPtr;
- SoundDataChunk SoundDataHeader, *SoundDataHeaderPtr;
- MarkerChunk MarkerChunkHeader, *MarkerChunkHeaderPtr;
- InstrumentChunk InstHeader, *InstHeaderPtr;
- long TotalSamps;
- extern Boolean AIFFoddByte;
- extern double srate; /* sampling rate */
- extern Str255 SoundFileName;
- extern ioParam myIOParmBlk;
- extern long SampleRate;
-
- Fixed aFix;
- Extended80 exNum;
-
-
- FormChunkHeaderPtr = &FormChunkHeader;
- CommonHeaderPtr = &CommonHeader;
- SoundDataHeaderPtr = &SoundDataHeader;
- aLong = (long)(TotalDuration * nchnls * srate);
- resid = (int)(aLong % 512L);
- if ( resid != 0 ) {
- aLong += (long)(512 - resid) + 512L;;
- }
- TotalSamps = aLong;
-
- /* initialize Form Chunk info */
- FormChunkHeader.ckID = 'FORM';
- FormChunkHeader.formType = 'AIFF';
-
-
- /* initialize Common Chunk info */
- CommonHeader.ckID = 'COMM';
- CommonHeader.ckSize = sizeof( CommonHeader.numChannels ) +
- sizeof( CommonHeader.numSampleFrames ) +
- sizeof( CommonHeader.sampleSize ) +
- sizeof( CommonHeader.sampleRate );
- CommonHeader.numChannels = (short)nchnls;
- CommonHeader.numSampleFrames = (unsigned long)(TotalSamps / nchnls);
- CommonHeader.sampleSize = SixteenBits;
-
- /* can't figure out how to convert sample rate TO extended format
- in version 3 of THINK C, so just shove in some numbers to fake it */
- CommonHeader.sampleRate.exponent = 0x400E;
- CommonHeader.sampleRate.mantissa.long1 = (long)srate;
- CommonHeader.sampleRate.mantissa.long2 = 0;
-
- aFix = X2Fix( &CommonHeader.sampleRate);
- SampleRate = (long) aFix;
-
-
- SoundDataHeader.ckID = 'SSND';
- SoundDataHeader.offset = 0L;
- SoundDataHeader.blockSize = 0L;
-
- SoundDataHeader.ckSize = sizeof( SoundDataHeader.offset ) +
- sizeof( SoundDataHeader.blockSize ) +
- (long)(TotalSamps * sizeof(int));
-
-
-
- FormChunkHeader.ckSize = sizeof(FormChunkHeader.formType);
-
- FormChunkHeader.ckSize += (sizeof(CommonHeader.ckID) +
- sizeof(CommonHeader.ckSize)
- + CommonHeader.ckSize);
-
- FormChunkHeader.ckSize += (sizeof(SoundDataHeader.ckID) +
- sizeof(SoundDataHeader.ckSize)
- + SoundDataHeader.ckSize);
-
- if ( FormChunkHeader.ckSize % 2 != 0 )
- AIFFoddByte = TRUE;
- else
- AIFFoddByte = FALSE;
-
-
- /* write out header info */
- myIOParmBlk.ioReqCount = sizeof(FormChunkHeader);
- myIOParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
- if ( (errnum = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- OSError("\pError writing Header to sample file", NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- OSError("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(CommonHeader);
- myIOParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- if ( (errnum = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- OSError("\pError writing Header to sample file", NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- OSError("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(SoundDataHeader);
- myIOParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
- if ( (errnum = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- OSError("\pError writing Header to sample file", NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- OSError("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
- }
-
-
- FixHeaderInfo()
- {
- /* this is a kludgey way to ensure that the AIFF file header
- * info is accurate. We just read it all back again from the file
- * and patch it up.
- */
-
- register i;
- Str255 myStr255;
- double x;
- extern OSErr errnum;
- extern double srate;
- long aLong;
- int resid;
- Chunk FormChunkHeader, *FormChunkHeaderPtr;
- CommonChunk CommonHeader, *CommonHeaderPtr;
- SoundDataChunk SoundDataHeader, *SoundDataHeaderPtr;
- MarkerChunk MarkerChunkHeader, *MarkerChunkHeaderPtr;
- InstrumentChunk InstHeader, *InstHeaderPtr;
- extern long TotalSamps;
- extern Boolean AIFFoddByte;
- extern double srate; /* sampling rate */
- extern Str255 SoundFileName;
- extern ioParam myIOParmBlk;
- extern int nchnls;
- extern long SampleRate;
-
- Fixed aFix;
- Extended80 exNum;
-
- FormChunkHeaderPtr = &FormChunkHeader;
- CommonHeaderPtr = &CommonHeader;
- SoundDataHeaderPtr = &SoundDataHeader;
-
-
- /* reset file position to start */
-
- myIOParmBlk.ioPosMode = fsFromStart;
- myIOParmBlk.ioPosOffset = 0L;
- errnum = PBSetFPos(&myIOParmBlk, FALSE);
- myIOParmBlk.ioPosMode = fsAtMark;
-
-
-
-
- /* initialize Form Chunk info */
- FormChunkHeader.ckID = 'FORM';
- FormChunkHeader.formType = 'AIFF';
-
-
- /* initialize Common Chunk info */
- CommonHeader.ckID = 'COMM';
- CommonHeader.ckSize = sizeof( CommonHeader.numChannels ) +
- sizeof( CommonHeader.numSampleFrames ) +
- sizeof( CommonHeader.sampleSize ) +
- sizeof( CommonHeader.sampleRate );
- CommonHeader.numChannels = (short)nchnls;
- CommonHeader.numSampleFrames = (unsigned long)(TotalSamps / nchnls);
- CommonHeader.sampleSize = SixteenBits;
-
-
- /* can't figure out how to convert sample rate TO extended format
- in version 3 of THINK C, so just shove in some numbers to set
- to 44100 */
- CommonHeader.sampleRate.exponent = 0x400E;
- CommonHeader.sampleRate.mantissa.long1 = (long)srate;
- CommonHeader.sampleRate.mantissa.long2 = 0;
-
- aFix = X2Fix( &CommonHeader.sampleRate);
- SampleRate = (long) aFix;
-
-
- SoundDataHeader.ckID = 'SSND';
- SoundDataHeader.offset = 0L;
- SoundDataHeader.blockSize = 0L;
-
- SoundDataHeader.ckSize = sizeof( SoundDataHeader.offset ) +
- sizeof( SoundDataHeader.blockSize ) +
- (long)(TotalSamps * sizeof(int));
-
-
-
- FormChunkHeader.ckSize = sizeof(FormChunkHeader.formType);
-
- FormChunkHeader.ckSize += (sizeof(CommonHeader.ckID) +
- sizeof(CommonHeader.ckSize)
- + CommonHeader.ckSize);
-
- FormChunkHeader.ckSize += (sizeof(SoundDataHeader.ckID) +
- sizeof(SoundDataHeader.ckSize)
- + SoundDataHeader.ckSize);
-
- if ( FormChunkHeader.ckSize % 2 != 0 )
- AIFFoddByte = TRUE;
- else
- AIFFoddByte = FALSE;
-
-
- /* REwrite out header info */
- myIOParmBlk.ioReqCount = sizeof(FormChunkHeader);
- myIOParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
- if ( (errnum = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- OSError("\pError writing Header to sample file", NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- OSError("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(CommonHeader);
- myIOParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- if ( (errnum = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- OSError("\pError writing Header to sample file", NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- OSError("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(SoundDataHeader);
- myIOParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
- if ( (errnum = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- OSError("\pError writing Header to sample file", NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- OSError("\pError writing Header to file, wrote wrong number of bytes", NIL);
- }
- }
-